EditorView = {}
rawset(EditorView, "__index", EditorView)

function CreateView(parent, rect)
	if rect == nil then
		return nil
	end

	local inst = {}
	setmetatable(inst, EditorView)
	local wnd = wx.wxWindow(parent, wx.wxID_ANY)
	if wnd == nil then
		return nil 
	end

	wnd:SetSize(rect)
	inst.window = wnd

	local render_target = ov.CreateRenderWindow("OgreRenderWindow", rect:GetWidth(), rect:GetHeight(), wnd:GetHandle())
	if render_target == nil then return nil end

	wnd:Connect(wx.wxEVT_DESTROY, function() inst:OnDestroy() end)

	inst.render_target= render_target

	wnd:Connect(wx.wxEVT_LEFT_DOWN, function(event) inst:OnLButtonDown(event) end)
	wnd:Connect(wx.wxEVT_LEFT_UP, function(event) inst:OnLButtonUp(event) end)
	wnd:Connect(wx.wxEVT_RIGHT_DOWN, function(event) inst:OnRButtonDown(event) end)
	wnd:Connect(wx.wxEVT_RIGHT_UP, function(event) inst:OnRButtonUp(event) end)
	wnd:Connect(wx.wxEVT_MOTION, function(event) inst:OnMouseMove(event) end)
	wnd:Connect(wx.wxEVT_KEY_DOWN, function(event) inst:OnKeyDown(event) end)
	wnd:Connect(wx.wxEVT_KEY_UP, function(event) inst:OnKeyUp(event) end)
	wnd:Connect(wx.wxEVT_MOUSEWHEEL, function(event) inst:OnMouseWheel(event) end)
	wnd:Connect(wx.wxEVT_KILL_FOCUS, function() inst:LostFocus() end)

	LoadModule("script/plugins/cameraplugin")
	inst.active_plugin = cameraplugin.CreatePlugin()
	inst.plugins = {}
	rawset(inst.plugins, "CameraPlugin", inst.active_plugin)

	return inst
end

function EditorView:LostFocus()
	if not self.active_plugin then return end
	self.active_plugin.LostFocus()
end

function EditorView:Update(time)
	if not self.active_plugin then return end
	self.active_plugin:Update(time)
end

function EditorView:OnDestroy()
	if self.render_target then ov.DestroyRenderWindow(self.render_target) end
end

function EditorView:OnMouseWheel(event)
	if not self.active_plugin then return end
	self.active_plugin:OnMouseWheel(event)
end

function EditorView:OnLButtonDown(event)
	if not self.active_plugin then return end
	self.window:CaptureMouse()
	self.active_plugin:OnLButtonDown(event)
end
function EditorView:OnLButtonUp(event)
	if not self.active_plugin then return end
	self.window:ReleaseMouse()
	self.active_plugin:OnLButtonUp(event)
end

function EditorView:OnRButtonDown(event)
	if not self.active_plugin then return end
	self.window:CaptureMouse()
	self.active_plugin:OnRButtonDown(event)
end
function EditorView:OnRButtonUp(event)
	if not self.active_plugin then return end
	self.window:ReleaseMouse()
	self.active_plugin:OnRButtonUp(event)
end

function EditorView:OnMouseMove(event)
	if self.active_plugin then self.active_plugin:OnMouseMove(event) end
end

function EditorView:OnKeyDown(event)
	if not self.active_plugin then return end
	self.active_plugin:OnKeyDown(event)
end

function EditorView:OnKeyUp(event)
	if not self.active_plugin then return end
	self.active_plugin:OnKeyUp(event)
end